package net.ak.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ak.model.Category;
import net.ak.model.UserTO;
import net.ak.response.AKResponse;
import net.ak.service.UserService;
import net.ak.utils.EncriptUtils;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	private AKResponse akResponse;
	
	private List<Category> categoryList;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView gotoLoginPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("login");

		return mv;
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public AKResponse login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String u 		= request.getParameter("u");
		String p 		= request.getParameter("p");
		String lat 		= request.getParameter("lat");
		String lng 		= request.getParameter("lng");
		System.out.println("-----------" + u + ":" + p + ":" + lat + ":" + lng);
		AKResponse akResponse = userService.login(u, EncriptUtils.getInstance().md5Encrypt(p), lat, lng);
		if (akResponse.getStatusCode().equalsIgnoreCase("200")) {
			System.out.println(akResponse.getResponseData());
			UserTO userTo = (UserTO)new ObjectMapper().readValue(akResponse.getResponseData().toString(), UserTO.class);
			response.setHeader("ak-url", "http://localhost:8081/ak-frontend/listAllCategory?session="+userTo.getSession());
			response.setHeader("session", userTo.getSession());
			
			//session = user
			request.getSession().setAttribute(userTo.getSession(),userTo);
		}
		System.out.println(akResponse);
		return akResponse;
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("register");

		return mv;
	}
	
	@RequestMapping(value = "/password-reset", method = RequestMethod.GET)
	public ModelAndView resetPassword(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("password-reset");

		return mv;
	}
	
	@RequestMapping(value = "/end-password-reset", method = RequestMethod.GET)
	public ModelAndView resetPasswordEnd(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("end-password-reset");

		return mv;
	}
	
	@RequestMapping(value = "/listAllCategory", method = RequestMethod.GET)
	public ModelAndView listAllCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String session = request.getParameter("session");
		AKResponse akResponse = userService.listAllCategory(session);
		System.out.println(akResponse);
		ModelAndView mv = new ModelAndView("listAllCategory");
		categoryList = Arrays.asList(new ObjectMapper().readValue(akResponse.getResponseData().toString(), Category[].class));
		mv.addObject("categoryList", categoryList);
		mv.addObject("session", session);
		System.out.println(categoryList);
		return mv;
	}

	@RequestMapping(value = "/selectCategory", method = RequestMethod.GET)
	public ModelAndView selectCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String session 	= request.getParameter("session");
		String catId 	= request.getParameter("catId");
		System.out.println("session -> " + session + " --- category id -->" + catId );
		AKResponse akResponse = userService.getCategoryById(session, catId);
		System.out.println(akResponse);
		ModelAndView mv = new ModelAndView("selectCategory");
		Category c 		= (Category)new ObjectMapper().readValue(akResponse.getResponseData().toString(), Category.class);
		mv.addObject("category", c);
		mv.addObject("session", session);
		return mv;
	}
	
	@RequestMapping(value = "/registerCategory", method = RequestMethod.GET)
	public ModelAndView registerCategoryPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String session 	= request.getParameter("session");
		String catId 	= request.getParameter("catId");
		
		System.out.println("session -> " + session + ": category id -->" + catId);
		AKResponse akResponse = userService.getCategoryById(session, catId);
		System.out.println(akResponse);
		ModelAndView mv = new ModelAndView("registerCategory");
		Category c 		= (Category)new ObjectMapper().readValue(akResponse.getResponseData().toString(), Category.class);
		mv.addObject("category", c);
		mv.addObject("session", session);
		return mv;
	}
	
	@RequestMapping(value = "/registerCategory", method = RequestMethod.POST)
	@ResponseBody
	public AKResponse registerCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String session 		= request.getParameter("session");
		String catId 		= request.getParameter("catId");
		String cost 		= request.getParameter("cost");
		String desc 		= request.getParameter("desc");
		String paypalEmail 	= request.getParameter("paypalEmail");
		
		System.out.println("session -> " + session + ": category id -->" + catId + ": cost  ->" + cost + ": desc -> " + desc);
		AKResponse akResponse = userService.registerCategory(session, catId, cost, desc, paypalEmail);
		System.out.println(akResponse);
		if (akResponse.getStatusCode().equalsIgnoreCase("200")) {
			System.out.println(akResponse.getResponseData());
			response.setHeader("ak-url", "http://localhost:8081/ak-frontend/listAllCategory?session="+session);
			response.setHeader("session", session);
		}
		return akResponse;
	}
	
	@RequestMapping(value = "/userProfile", method = RequestMethod.GET)
	public ModelAndView userProfile(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("userProfile");
		return mv;
	}
	
	@RequestMapping(value = "/seeUser", method = RequestMethod.GET)
	public ModelAndView seeUser(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("seeUser");
		return mv;
	}
	
	
	/* ************************ SETTER AND GETTER ****************************** */
	/**
	 * @return the akResponse
	 */
	public AKResponse getAkResponse() {
		return akResponse;
	}

	/**
	 * @param akResponse the akResponse to set
	 */
	public void setAkResponse(AKResponse akResponse) {
		this.akResponse = akResponse;
	}

	/**
	 * @return the categoryList
	 */
	public List<Category> getCategoryList() {
		return categoryList;
	}

	/**
	 * @param categoryList the categoryList to set
	 */
	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}
}
